<template>
  <div class="web-container info">
    <div class="content">
      <div class="content-left">
        <div class="content-top">
          <span class="content-cover" :style="{backgroundImage:'url('+bookDetails.cover+')'}"></span>
          <div class="content-top-right">
            <span class="content-title" v-html="bookDetails.bookName">
            </span>
            <span class="content-author" v-html="bookDetails.author"></span>
            <span class="content-press" v-html="bookDetails.press"></span>
            <span class="content-year" >{{bookDetails.publishingDate|dataYMD}}</span>
            <span class="content-read">{{collect.readreCount}}人在读</span>
            <div class="content-btn">
              <el-button type="primary" round @click="goToContent">阅&nbsp;读</el-button>
              <ul>
                <li @click="toggleCollec(collect.whetherCollection)" :class="collect.whetherCollection==1 ? 'active':''"><i class="iconfont icon-shoucang"></i>收藏</li>
                <li>
                   <!-- <el-button type="primary" round @click="addBooks" v-if="collect.whetherBookShel == 0">加入书架</el-button> -->
                   <div class="sc" @click="addBooks" v-if="collect.whetherBookShel == 0"> <img src="/static/images/base/wsc.png" alt=""> 加入书架</div>
                   <div class="sc" style="color:#e32e25" @click="deleteBooks" v-else> <img src="/static/images/base/ysc.png" alt=""> 移除书架</div>
                  <!-- <el-button type="primary" class="active" v-else round @click="deleteBooks">移除书架</el-button> -->
                </li>
                <!-- <li><i class="iconfont icon-xiazai"></i>下载.</li> -->
              </ul>
            </div>
          </div>
        </div>
        <div class="content-bottom">
          <span class="title">简介</span>
          <div class="desc" v-html="bookDetails.bookIntroduction">
          </div>
        </div>
      </div>
      <div class="content-right">
        <div class="read-box">
          <el-tabs v-model="activeName2" type="card" >
            <el-tab-pane label="移动端阅读" name="first">
              <span class="scan">
                <img  :src="bookcode" style="width: 135px;" alt="">
              </span>
              <span class="scan">扫描二维码</span>
              </el-tab-pane>
            <el-tab-pane label="关注公众号" name="second">
             <span class="scan">
                <img  src="/static/images/base/code.jpg" style="width: 135px;" alt="">
              </span>
              <span class="scan">扫描关注公众号</span>
            </el-tab-pane>
          </el-tabs>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { GetBookInfoList, GetBookCode } from "@/api/Api";
import {
  isCollect,
  addCollect,
  delCollect,
  addbooks,
  deleteBookShelf
} from "@/api/index";
import moment from "moment";
export default {
  data() {
    return {
      fileType: undefined,
      bookDetails: { filesPathList: [{ fileResourcePath: null }] },
      downText: "下载",
      dwonType: false,
      bookcode: undefined,
      total: 0,
      variable: 0,
      percentage: 0,
      percentageFlag: false,
      loading: true,
      collect: {},
      activeName2: "first"
    };
  },
  //时间过滤器
  filters: {
    dataYMD: function(el) {
      return moment(el).format("YYYY-MM-DD");
    },
  },
  created() {
    this.isCollect();
    GetBookInfoList(this.$route.query.bookId)
      .then(result => {
        this.bookDetails = result.data;
        console.log(result.data.filesPathList.length)
        this.fileType = result.data.format[0];
      })
      .then(() => {
        GetBookCode({ type: "1", bookId: "" + this.$route.query.bookId }).then(
          result => {
            // console.log(result)
            this.bookcode = "data:image/png;base64," + result.data;
          }
        );
      });
  },
  components: {},
  methods: {
    //移除书架
    deleteBooks() {
      if(this.$store.state.isLogin == 0){
         this.$store.commit("SET_LOGINSHOW", true);
         return;
      }
      deleteBookShelf(this.collect.bookShelfId).then(res => {
        console.log(res.data);
        this.isCollect();
      });
    },
    //添加到书架
    addBooks() {
      if(this.$store.state.isLogin == 0){
         this.$store.commit("SET_LOGINSHOW", true);
         return;
      }
      let book = this.bookDetails;
      addbooks(book.bookIntroduction,book.cover,book.bookName,book.author,book.bookId
      ).then(res => {
        console.log(res.data);
        this.isCollect();
      });
    },
    //是否收藏 添加 阅读数
    isCollect() {
      if(this.$store.state.isLogin == 0){
         this.$store.commit("SET_LOGINSHOW", true);
         return;
      }
      isCollect(this.$route.query.bookId).then(res => {
        console.log(res.data.date);
        this.collect = res.data.date;
      });
    },
    //查询是否收藏
    toggleCollec(is) {
      if (is == 0) {
        addCollect(1,this.bookDetails.cover,this.bookDetails.bookName,this.bookDetails.bookIntroduction,this.bookDetails.bookId).then(res => {
           this.$message({
              message: '收藏成功',
              type: 'success',
              center: true
            });
        });
      } else {
        delCollect(this.collect.collectionId).then(res => {
          this.$message({
              message: '移除收藏',
              type: 'success',
              center: true
            });
        });
      }
      this.isCollect();
    },
    goToContent() {
      if(this.$store.state.isLogin == 0){
         this.$store.commit("SET_LOGINSHOW", true);
         return;
      }
      if (this.fileType === "Epub") {
        debugger
        if(this.bookDetails.filesPathList.length == 0){
          this.$message.error('当前图书无下载地址');
          return
        }
        this.$router.push("/book/content?bookId=" + this.bookDetails.bookId);
      } else {
        this.$router.push("/periodical/content?url=" +this.bookDetails.filesPathList[0].fileResourcePath);
      }
    }
  }
};
</script>
<style lang="less" scoped>
.custom-dz{
  .info .content .content-left .content-bottom .title{
    color: #e93926;
  }
  .info .content .content-right .read-box{
    background: #fff1ef;
  }
}
.info {
  .content {
    min-height: 400px;
    background: #fff;
    border-radius: 10px;
    padding: 30px 128px;
    display: flex;
    .content-left {
      flex: 1;
      .content-top {
        display: flex;
        .content-cover {
          background: url("/static/images/base/book.png") no-repeat;
          background-size: cover;
          margin-bottom: 34px;
          width: 196px;
          height: 264px;
          border-radius: 10px;
          box-shadow: 0 2px 10px rgba(0,0,0,0.4);
        }
        .content-top-right {
          margin-left: 30px;
          height: 264px;
          display: flex;
          flex: 1;
          width: 100%;
          flex-direction: column;
          .content-btn {
            margin-top: 110px;
            display: flex;
            li.active{
              color:red;
              .icon-shoucang{
                color: red;
              }
            }
            .el-button {
              // background-color: #15c798;
              line-height: 32px;
              width: 110px;
              font-size: 16px;
              padding: 0px 20px;
              border: 2px solid transparent;
              .el-button--primary:focus,
              .el-button--primary:hover {
                // background-color: #15c798;
              }
            }
            .el-button.active {
              background-color: red;
            }
            ul {
              padding-left: 0px;
              width: 100%;
              text-align: right;
              display: flex;
              justify-content: flex-end;
              li {
                // float: left;
                cursor: pointer;
                line-height: 42px;
                font-size: 16px;
                color: #808080;
                display: flex;
                align-items: center;
                padding: 0 15px;
                i {
                  font-size: 30px;
                  margin-right: 5px;
                }
                .sc{
                  display: flex;
                  align-items: center;
                  img{
                    margin-right:5px;
                  }
                }
              }
            }
          }

          .content-title {
            font-size: 24px;
            font-weight: bold;
          }
          .content-author,
          .content-press,
          .content-year,
          .content-read {
            font-size: 16px;
            color: #808080;
            margin-top: 5px;
          }
        }
      }
      .content-bottom {
        .title {
          display: block;
          padding: 15px 25px;
          font-size: 16px;
          color: #15c798;
          font-weight: bold;
          border-bottom: 1px solid #c1c1c1;
          position: relative;
        }
        .title::after {
          content: "";
          display: inline-block;
          width: 8px;
          height: 8px;
          background: #fff;
          border-top: 1px solid #c1c1c1;
          border-right: 1px solid #c1c1c1;
          transform: rotate(-45deg);
          position: absolute;
          bottom: -5px;
          left: 35px;
        }
        .desc {
          padding: 30px 25px 150px 25px;
          font-size: 16px;
          line-height: 30px;
          text-align: justify;
          color: #a1a1a1;
        }
      }
    }
    .content-right {
      width: 223px;
      margin-left: 30px;
      user-select: none;
      .read-box {
        width: 100%;
        border-radius: 10px;
        background: #ecfcf8;
        box-shadow: #e6e6e6 2px 2px 10px;
        .scan {
          display: block;
          text-align: center;
          font-size: 14px;
          padding: 6px 0;
          color: #808080;
        }
        .title {
          display: block;
          padding: 20px 0px 14px 20px;
          font-size: 18px;
          font-weight: bold;
          border-bottom: 1px solid #c8e2dc;
        }
        .code {
          margin-left: 52px;
        }
        .system {
          display: flex;
          justify-content: space-between;
          padding: 20px 45px;
          cursor: pointer;
          span {
            display: flex;
            flex-direction: column;
            align-items: center;
            color: #15c798;
            img {
              margin-bottom: 10px;
            }
          }
        }
      }
    }
  }
}
</style>


